package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.mapper.LeaseTermMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Override
    public AgreementVo getAgreementById(Long id) {
        LeaseAgreement agreement = getById(id);
        LambdaQueryWrapper<ApartmentInfo> apartmentInfoWrapper=new LambdaQueryWrapper<>();
        apartmentInfoWrapper.eq(ApartmentInfo::getId,agreement.getApartmentId());
        ApartmentInfo apartmentInfo = apartmentInfoService.getOne(apartmentInfoWrapper);
        LambdaQueryWrapper<RoomInfo> roomInfoWrapper=new LambdaQueryWrapper<>();
        roomInfoWrapper.eq(RoomInfo::getId,agreement.getRoomId());
        RoomInfo roomInfo = roomInfoService.getOne(roomInfoWrapper);
        LambdaQueryWrapper<PaymentType> paymentTypeWrapper=new LambdaQueryWrapper<>();
        paymentTypeWrapper.eq(PaymentType::getId,agreement.getPaymentTypeId());
        PaymentType paymentType = paymentTypeService.getOne(paymentTypeWrapper);
        LambdaQueryWrapper<LeaseTerm> leaseTermWrapper=new LambdaQueryWrapper<>();
        leaseTermWrapper.eq(LeaseTerm::getId,agreement.getLeaseTermId());
        LeaseTerm leaseTerm = leaseTermService.getOne(leaseTermWrapper);
        AgreementVo agreementVo=new AgreementVo();
        agreementVo.setLeaseTerm(leaseTerm);
        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setPaymentType(paymentType);
        agreementVo.setRoomInfo(roomInfo);
        BeanUtils.copyProperties(agreement,agreementVo);

        return agreementVo;
    }

    @Override
    public void listAgreement(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
        page=leaseAgreementMapper.listAgreement(page,queryVo);
    }
}




